/**************************************************************************************************
	$Header: /pub/cvsroot/mydns/src/lib/ptr.c,v 1.10 2002/07/04 10:47:11 bboy Exp $

	Copyright (C) 2002  Don Moore <bboy@bboy.net>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at Your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
**************************************************************************************************/

#include "mydns.h"

#define MYDNS_PTR_FIELDS	"id,ip,name,ttl"


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	_MYDNS_PARSE_PTR
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline MYDNS_PTR *
_mydns_parse_ptr(row)
	MYSQL_ROW row;
{
	MYDNS_PTR *rv;

	if ((rv = (MYDNS_PTR *)calloc(1, sizeof(MYDNS_PTR))))
	{
#ifdef MEMDEBUG_LIB
		memdebug(rv, "+ MYDNS_PTR");
#endif
		rv->next = NULL;

		rv->id = atou(row[0]);
		rv->ip = atou(row[1]);
		strncpy(rv->name, row[2], sizeof(rv->name)-1);
		rv->ttl = atou(row[3]);
	}
	return (rv);
}
/*-----------------------------------------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	MYDNS_PTR_DUP
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
MYDNS_PTR *
mydns_ptr_dup(start, recurse)
	MYDNS_PTR *start;
	int recurse;
{
	register MYDNS_PTR *first = NULL, *last = NULL, *ptr, *s, *tmp;

	for (s = start; s; s = tmp)
	{
		tmp = s->next;

		if ((ptr = (MYDNS_PTR *)malloc(sizeof(MYDNS_PTR))))
		{
#ifdef MEMDEBUG_LIB
			memdebug(ptr, "+ MYDNS_PTR");
#endif
			ptr->id = s->id;
			ptr->ip = s->ip;
			strncpy(ptr->name, s->name, sizeof(ptr->name)-1);
			ptr->ttl = s->ttl;
			ptr->next = NULL;
			if (recurse)
			{
				if (!first) first = ptr;
				if (last) last->next = ptr;
				last = ptr;
			}
			else
				return (ptr);
		}
	}
	return (first);
}
/*-----------------------------------------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	MYDNS_PTR_SIZE
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline size_t
mydns_ptr_size(first)
	MYDNS_PTR *first;
{
	register MYDNS_PTR *p;
	register size_t size = 0;

	for (p = first; p; p = p->next)
		size += sizeof(MYDNS_PTR) + strlen(p->name);

	return (size);
}
/*-----------------------------------------------------------------------------------------------*/



/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	_MYDNS_PTR_FREE
	Frees the pointed-to structure.	Don't call this function directly, call the macro.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline void
_mydns_ptr_free(first)
	MYDNS_PTR *first;
{
	register MYDNS_PTR *p, *tmp;

	for (p = first; p; p = tmp)
	{
		tmp = p->next;

#ifdef MEMDEBUG_LIB
		memdebug(p, "- MYDNS_PTR");
#endif
		free(p);
	}
}
/*-----------------------------------------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	MYDNS_PTR_LOAD
	Returns 0 on success or nonzero if an error occurred.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
int
mydns_ptr_load(mysql, rptr, addr)
	MYSQL	*mysql;					/* Open MySQL connection */
	MYDNS_PTR **rptr;				/* Result list will be returned here */
	struct in_addr *addr;		/* Look up PTR record for this IP */
{
	MYDNS_PTR *first = NULL, *last = NULL;
	size_t	 querylen;
	char		 query[DNS_QUERYBUFSIZ];
	MYSQL_RES *res;
	MYSQL_ROW row;

	MYDNS_CLEARERR(mysql)

	/* Verify args */
	if (!mysql || !rptr)
	{
		errno = EINVAL;
		return (-1);
	}

	/* Construct query */
	querylen = snprintf(query, sizeof(query),
		"SELECT "MYDNS_PTR_FIELDS" FROM "MYDNS_PTR_TABLE" WHERE ip=%u", addr->s_addr);

	/* Submit query */
	if (mysql_real_query(mysql, query, querylen) || !(res = mysql_store_result(mysql)))
		return (-1);

	/* Add results to list */
	while ((row = mysql_fetch_row(res)))
	{
		MYDNS_PTR *new;

		if (!(new = _mydns_parse_ptr(row)))
		{
			mysql_free_result(res);
			return (-1);
		}
		if (!first) first = new;
		if (last) last->next = new;
		last = new;
	}
	*rptr = first;
	mysql_free_result(res);
	return (0);
}
/*-----------------------------------------------------------------------------------------------*/

/* vi:set ts=3: */
